package com.stu.day17;

/**
 * 线程的礼让 - 让出cpu时间片 - 至于cpu时间片继续给哪个线程 - 说不定的
 * @author zy
 * @date 2025/2/18 11:27
 */
public class YieldDemo extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+":"+i);

            if(i==2){
                System.out.println(Thread.currentThread().getName()+"让出cpu时间片!");
                Thread.yield();
            }
        }
    }

    public static void main(String[] args) {
        Thread t1 = new YieldDemo();
        Thread t2 = new YieldDemo();
        t1.start();
        t2.start();
    }
}
